﻿using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;

namespace ZhongJia.Lock.Infrastructure.Utility
{
    public static  class EncryptHellper
    {
        public static string MD5EncryptHexString(string input, Encoding encoding = null)
        {
            if (string.IsNullOrEmpty(input))
                throw new ArgumentNullException(nameof(input));

            if (encoding == null)
                encoding = Encoding.UTF8;

            var data = encoding.GetBytes(input);
            var encryData = MD5Encrypt(data);

            StringBuilder sBuilder = new StringBuilder();
            for (int i = 0; i < encryData.Length; i++)
            {
                sBuilder.Append(encryData[i].ToString("x2"));
            }

            return sBuilder.ToString();
        }
   
        public static byte[] MD5Encrypt(byte[] bytes)
        {
            if (bytes == null || bytes.Length == 0)
                throw new ArgumentNullException("bytes", "MD5加密的字节不能为空！");

            using (MD5 md5Hash = MD5.Create())
            {
                return md5Hash.ComputeHash(bytes);
            }
        }
    }
}
